<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="设备ID" prop="deviceId">
						<el-input v-model="form.deviceId" placeholder="请输入设备ID" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="IMEI" prop="imei">
						<el-input v-model="form.imei" placeholder="请输入IMEI" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="设备类型" prop="deviceTypeId">
						<el-input v-model="form.deviceTypeId" placeholder="请输入设备类型" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="设备类型" prop="deviceTypeName">
						<el-input v-model="form.deviceTypeName" placeholder="请输入设备类型" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="安装点位" prop="installLocationId">
						<el-input v-model="form.installLocationId" placeholder="请输入安装点位" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="工单类型（事件类型）" prop="eventTypeId">
						<el-input v-model="form.eventTypeId" placeholder="请输入工单类型（事件类型）" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="报警原因" prop="alarmCauses">
						<el-input v-model="form.alarmCauses" placeholder="请输入报警原因" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="报警确认类型 10误报 20 演练 30 报警 40 其他" prop="confirmState">
						<el-input v-model="form.confirmState" placeholder="请输入报警确认类型 10误报 20 演练 30 报警 40 其他" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="备注" prop="remarks">
						<el-input v-model="form.remarks" placeholder="请输入备注" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="排序" prop="sort">
						<el-input v-model="form.sort" placeholder="请输入排序" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="状态 10 已处理  20 未处理" prop="state">
						<el-input v-model="form.state" placeholder="请输入状态 10 已处理  20 未处理" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="设备型号id" prop="deviceModelId">
						<el-input v-model="form.deviceModelId" placeholder="请输入设备型号id" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="单位id" prop="deptId">
						<el-input v-model="form.deptId" placeholder="请输入单位id" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="短信报警" prop="smsAlarmId">
						<el-input v-model="form.smsAlarmId" placeholder="请输入短信报警" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="短信简述" prop="smsAlarmSketch">
						<el-input v-model="form.smsAlarmSketch" placeholder="请输入短信简述" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="公众号报警" prop="officialAccountAlarmId">
						<el-input v-model="form.officialAccountAlarmId" placeholder="请输入公众号报警" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="公众号简述" prop="officialAccountSketch">
						<el-input v-model="form.officialAccountSketch" placeholder="请输入公众号简述" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="电话报警" prop="telephoneAlarmId">
						<el-input v-model="form.telephoneAlarmId" placeholder="请输入电话报警" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="电话简述" prop="telephoneAlarmSketch">
						<el-input v-model="form.telephoneAlarmSketch" placeholder="请输入电话简述" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="AlarmFaultLoggingDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/alarm/alarmFaultLogging';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	deviceId: '',
	imei: '',
	deviceTypeId: '',
	deviceTypeName: '',
	installLocationId: '',
	eventTypeId: '',
	alarmCauses: '',
	confirmState: '',
	remarks: '',
	sort: '',
	state: '',
	deviceModelId: '',
	deptId: '',
	smsAlarmId: '',
	smsAlarmSketch: '',
	officialAccountAlarmId: '',
	officialAccountSketch: '',
	telephoneAlarmId: '',
	telephoneAlarmSketch: '',
});

// 定义校验规则
const dataRules = ref({
	deviceId: [{ required: true, message: '设备ID不能为空', trigger: 'blur' }],
	imei: [{ required: true, message: 'IMEI不能为空', trigger: 'blur' }],
	deviceTypeId: [{ required: true, message: '设备类型不能为空', trigger: 'blur' }],
	deviceTypeName: [{ required: true, message: '设备类型不能为空', trigger: 'blur' }],
	installLocationId: [{ required: true, message: '安装点位不能为空', trigger: 'blur' }],
	eventTypeId: [{ required: true, message: '工单类型（事件类型）不能为空', trigger: 'blur' }],
	alarmCauses: [{ required: true, message: '报警原因不能为空', trigger: 'blur' }],
	confirmState: [{ required: true, message: '报警确认类型 10误报 20 演练 30 报警 40 其他不能为空', trigger: 'blur' }],
	remarks: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
	sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
	state: [{ required: true, message: '状态 10 已处理  20 未处理不能为空', trigger: 'blur' }],
	deviceModelId: [{ required: true, message: '设备型号id不能为空', trigger: 'blur' }],
	deptId: [{ required: true, message: '单位id不能为空', trigger: 'blur' }],
	smsAlarmId: [{ required: true, message: '短信报警不能为空', trigger: 'blur' }],
	smsAlarmSketch: [{ required: true, message: '短信简述不能为空', trigger: 'blur' }],
	officialAccountAlarmId: [{ required: true, message: '公众号报警不能为空', trigger: 'blur' }],
	officialAccountSketch: [{ required: true, message: '公众号简述不能为空', trigger: 'blur' }],
	telephoneAlarmId: [{ required: true, message: '电话报警不能为空', trigger: 'blur' }],
	telephoneAlarmSketch: [{ required: true, message: '电话简述不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取alarmFaultLogging信息
	if (id) {
		form.id = id;
		getalarmFaultLoggingData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getalarmFaultLoggingData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
